package JUC;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author 豆包LU
 * @date 2023/5/8 9:46
 */
public class SynchronousQueueDemo {
    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue = new SynchronousQueue<String>();
        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName() +"put1");
                        blockingQueue.put("1");
                String element = blockingQueue.element();
                System.out.println(Thread.currentThread().getName() +" put 2");
                        blockingQueue.put("2");
                System.out.println(Thread.currentThread().getName() +" put 3");
                        blockingQueue.put("3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "师傅").start();
        new Thread(()->{
            try {
                //耗时，5秒钟 来⼀个顾客买包⼦
                try { TimeUnit.SECONDS.sleep(5); } catch
                (InterruptedException e) {e.printStackTrace(); }
                System.out.println( Thread.currentThread().getName()+"\t ==="+ blockingQueue.take());
                        //耗时，5秒钟 来⼀个顾客买包⼦
                try { TimeUnit.SECONDS.sleep(5); } catch
                (InterruptedException e) {e.printStackTrace(); }
                System.out.println( Thread.currentThread().getName()+"\t ==="+ blockingQueue.take());
                        //耗时，5秒钟 来⼀个顾客买包⼦
                try { TimeUnit.SECONDS.sleep(5); } catch
                (InterruptedException e) {e.printStackTrace(); }
                System.out.println( Thread.currentThread().getName()+"\t ==="+ blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "顾客").start();
    }
}